package com.qf.shopping.servlet.frontController;

import com.alibaba.fastjson.JSON;
import com.qf.shopping.common.Constants;
import com.qf.shopping.common.DisatpcherServlet;
import com.qf.shopping.entity.Address;
import com.qf.shopping.entity.ShoppingCart;
import com.qf.shopping.entity.User;
import com.qf.shopping.service.IAddressService;
import com.qf.shopping.service.ICartService;
import com.qf.shopping.service.IOrderService;
import com.qf.shopping.service.impl.AddressServiceImpl;
import com.qf.shopping.service.impl.CartServiceImpl;
import com.qf.shopping.service.impl.OrderServiceImpl;
import com.qf.shopping.utils.ResultResponse;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**订单交易的模块
 * @author 三旬
 * @version 1.0  2022/9/23
 */
@WebServlet("/orderController/*")
public class OrderController extends DisatpcherServlet {
    //创建访问业务层的对象
    IOrderService orderService = new OrderServiceImpl();
    IAddressService addressService = new AddressServiceImpl();
    ICartService cartService = new CartServiceImpl();

    /**
     * 结算商品，生成订单信息
     * @return
     */
    public String initMyOrder(HttpServletRequest request,HttpServletResponse response){
        //初始化订单
        //0、从session中获得用户的信息
        User user = (User) request.getSession().getAttribute("user");
        if(user != null){
            //1、获得用户的地址列表
            List<Address> addresses = addressService.getMyAddress(user.getId());
            //2、获得用户购物车的信息
            List<ShoppingCart> carts = cartService.getMyCart(user.getId());
            if(carts.size() != 0){
                //3、将地址、购物车放进作用域中
                request.setAttribute("addresses",addresses);
                request.setAttribute("carts",carts);
                //4、转发到订单确认页面
                return Constants.FORWARD+":"+"pay.jsp";
            }
        }
        return Constants.SCRIPT+":"+"您还没有宝贝，快去商品列表看看吧！";

    }

    /**
     * 生成订单
     * @param request
     * @param response
     * @return
     */
    public String createOrder(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //接收订单的消息
        String json = request.getReader().readLine();
        // 对象可以用Map来表示临时对象
        // 把JSON字符串中的内容转到Map中
        Map map = JSON.parseObject(json, Map.class);// 地址id，支付方式，物流方式
        // 获取数据
        User user = (User) request.getSession().getAttribute("user");
        Integer addressId = Integer.valueOf(map.get("addressId").toString());
        String bank = (String) map.get("bank");
        String express = (String) map.get("express");

        // 调用业务层生成订单，返回订单编号
        Integer oid = orderService.createOrder(user.getId(), addressId, bank, express);
        if(oid > 0){
            return Constants.JSON+":"+ ResultResponse.success("订单提交成功！您的订单编号是："+oid+"，请及时支付，我们将尽快发货");
        }
        return Constants.JSON+":"+ ResultResponse.Error("订单提交失败！系统故障，请尽快与开发人员联系");
    }

}
